package _17_函数式编程.Lambda._7_使用Lambda;

import _17_函数式编程.Lambda.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _7_使用Lambda {

    public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(
                new Apple("red",150),
                new Apple("green",234),
                new Apple("green",123)
        );

        List<Apple> result;

        result = filterApples(appleList,
                (Apple apple) -> "green".equals(apple.getColor())
                );
        printApples(result,
                (Apple apple) -> "苹果的颜色：" + apple.getColor() + "，重量：" + apple.getWeight()
                );
    }

    /**
     * 筛选苹果
     * @param inventory
     * @param applePredicate
     * @return
     */
    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory){
            if(applePredicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

    /**
     * 打印苹果信息
     * @param inventory
     * @param appleFormatter
     */
    public static void printApples(List<Apple> inventory, AppleFormatter appleFormatter){
        for (Apple apple : inventory){
            System.out.println(appleFormatter.printApple(apple));
        }
    }
}
